package nbcp.myoql.code.generator.tool.freemarker

import nbcp.base.extend.*
import nbcp.myoql.code.generator.db.mysql.EntityDbItemFieldData

class FreemarkerColumnStyle : BaseMethodModelFreemarker() {

    private fun String.wrap(wrap: String): String {
        if (this.isEmpty()) {
            return "";
        }

        return wrap + this + wrap;
    }

    override fun exec(list: MutableList<Any?>): Any {
        var paramValue = getFreemarkerParameter(list[2]).AsString().trim();

        var key = getFreemarkerParameter(list[1]).AsString().trim();
        var field = getFreemarkerParameter(list[0]) as EntityDbItemFieldData;

        if (field.isPrimary) {
            paramValue = paramValue.wrap("**")
        } else if (field.fieldName.IsIn("create_at", "create_by", "update_at", "update_by", "is_deleted")) {
            paramValue = paramValue.wrap("*")
        } else if (field.isUnique && key.IsIn("name", "extra")) {
            paramValue = paramValue.wrap("**")
        } else if (field.isVirtual) {
            if (key == "name") {
                paramValue = paramValue.wrap("**")
            }

            if (key == "extra") {
                paramValue += "**[计算列]**"
            }
        }

        return paramValue;
    }
}